import type { IMiddleware } from "koa-router";
import { makeResponse, HttpStatusCode } from "../util/response";

export const errorHandler: IMiddleware = async (ctx, next) => {
  try {
    await next();
  } catch (error) {
    if (error.status == 401)
      makeResponse(ctx, HttpStatusCode.Unauthorized, "权限不足");
    else makeResponse(ctx, HttpStatusCode.InternalServerError, error.message);
  }
};
